#define _CRT_SECURE_NO_WARNINGS 1
#include "SingleList.h"

////链表的打印
//void SingleListPrint(SingleListNode* phead)
//{
//	SingleListNode* current = phead;
//	while (current)
//	{
//		printf("%d->", current->data);
//		current = current->next;
//	}
//	printf("NULL\n");
//}

//链表节点的创建
SingleListNode* CreateNode(Datatype x)
{
	SingleListNode* newnode = (SingleListNode*)malloc(sizeof(SingleListNode));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	newnode->data = x;
	//newnode->data.age = x.age;
	//newnode->data.address = x.address;
	//newnode->data.name = x.name;
	//newnode->data.phonenumber = x.phonenumber;
	//newnode->data.sex = x.sex;
	newnode->next = NULL;

	return newnode;
}

//尾插
void SingleListPushBack(SingleListNode** pphead,Datatype x)
{
	assert(pphead);

	//创建新节点
	SingleListNode* newnode = CreateNode(x);

	//判断链表是否为空
	if (*pphead == NULL)
	{
		//空链表
		*pphead = newnode;
	}
	else
	{
		//非空链表
		//找到尾节点
		SingleListNode* tail = *pphead;
		while (tail->next)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

//头插
void SingleListPushFront(SingleListNode** pphead, Datatype x)
{
	assert(pphead);

	//创建新节点
	SingleListNode* newnode = CreateNode(x);

	//newnode *pphead
	newnode->next = *pphead;
	*pphead = newnode;
}

//尾删
void SingleListDeleteBack(SingleListNode** pphead)
{
	assert(pphead && *pphead);//链表不能为空

	if ((*pphead)->next == NULL)
	{
		//链表只有一个节点
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		//链表有多个节点
		//找到尾节点
		SingleListNode* tail = *pphead;
		SingleListNode* prev = *pphead;//尾节点的前一个节点
		while (tail->next)
		{
			prev = tail;
			tail = tail->next;
		}
		//此时prev就是尾节点的前一个节点,tail就是尾节点
		free(tail);
		tail = NULL;
		prev->next = NULL;
	}
}

//头删
void SingleListDeleteFront(SingleListNode** pphead)
{
	assert(pphead && *pphead);

	SingleListNode* current = (*pphead)->next;
	free(*pphead);
	*pphead = current;
}

//查找
SingleListNode* SingleListFind(SingleListNode* phead,Datatype x)
{
	assert(phead);

	SingleListNode* current = phead;
	while (current)
	{
		if (0 == strcmp(current->data.name,x.name))
		{
			//找到了
			//printf("找到了！");
			return current;
		}
		current = current->next;
	}
	//出循环说明找不到
	//printf("找不到！");
	return NULL;
}

//在指定位置之前插入数据
void SingleListInsertFront(SingleListNode** pphead, SingleListNode* designate, Datatype x)
{
	assert(pphead && *pphead);

	//如果指定位置是第一个节点，则就是头插
	if (designate == *pphead)
	{
		//头插
		SingleListPushFront(pphead, x);
	}
	else
	{
		SingleListNode* newnode = (SingleListNode*)malloc(sizeof(SingleListNode));
		newnode->data = x;
		newnode->next = NULL;
		//找到指定位置的前一个结点
		SingleListNode* prev = *pphead;
		while (prev->next != designate)
		{
			prev = prev->next;
		}
		//prev newnode designate
		newnode->next = designate;
		prev->next = newnode;
	}
}

//在指定位置之后插入数据
void SingleListInsertBack(SingleListNode* designate, Datatype x)
{
	assert(designate);

	SingleListNode* newnode = CreateNode(x);
	//找到指定节点的下一个节点
	SingleListNode* next = designate->next;
	//designate newnode next
	newnode->next = next;
	designate->next = newnode;
}

//删除pos节点
void SingleListDeletePos(SingleListNode** pphead, SingleListNode* pos)
{
	assert(pphead && *pphead);
	assert(pos);

	if (pos == *pphead)
	{
		//pos节点是第一个节点，直接调用头删函数
		SingleListDeleteFront(pphead);
	}
	else
	{
		//删除pos节点需要知道pos的前一个节点和后一个节点
		SingleListNode* prev = *pphead;
		SingleListNode* Next = pos->next;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		//prev pos Next
		prev->next = Next;
		free(pos);
		pos = NULL;
	}
}

//删除pos节点之后的节点
void SingleListDeletePosBack(SingleListNode* pos)
{
	assert(pos && pos->next);
	SingleListNode* del = pos->next;
	SingleListNode* Next = del->next;
	//pos del Next
	pos->next = Next;
	free(del);
	del = NULL;
}

//销毁链表
void SingleListDestory(SingleListNode** pphead)
{
	assert(pphead && *pphead);

	SingleListNode* current = *pphead;
	while (current)
	{
		SingleListNode* next = current->next;
		free(current);
		current = next;
	}
	*pphead = NULL;
}